/*
 * @copyright: Huang Ding
 * @Author: ding-cx
 * @Date: 2021-08-20 14:05:47
 * @LastEditors: ding
 * @LastEditTime: 2022-09-26 18:27:02
 * @Description: 海清机器，时间、重启、等设备设置接口的封装tools工具
 */
import { hc } from "../hc";
import { actions } from "./actions";
import BaseHttpService from "./BaseHttpService";
import { DateFormat } from "@diyaner/ding";

/** 海清机器常用工具处理 */
export default class ToolsService extends BaseHttpService {
	/** 获取设备系统时间 */
	async getSysTime() {
		const operator = actions.GetSysTime;
		const res = await this.request<{
			operator: typeof operator;
			info: hc.http.dateTime;
		}>(operator, {});
		if (res) {
			const deviceTime = res.info;
			const formatTime1 = `${deviceTime.Year}-${deviceTime.Month}-${deviceTime.Day} ${deviceTime.Hour}:${deviceTime.Minute}:${deviceTime.Second}`;
			deviceTime.dateTime = DateFormat.init(formatTime1).format();
			return deviceTime;
		} else {
			this.app.logger.error("获取设备时间失败! %s", this.device.ip);
			throw new Error("获取机器时间失败！" + this.device.ip);
		}
	}

	/**
	 * 设置设备系统时间
	 *
	 * @param {hc.http.dateTime} [outDate] 外部时间 可选
	 * @return {*}
	 */
	async setSysTime(outDate?: hc.http.dateTime) {
		const operator = actions.SetSysTime;
		let data;
		if (outDate) {
			data = {
				info: outDate,
			};
		} else {
			const df = new Date();
			data = {
				info: {
					Year: df.getFullYear(),
					Month: df.getMonth() + 1,
					Day: df.getDate(),
					Hour: df.getHours(),
					Minute: df.getMinutes(),
					Second: df.getSeconds(),
				},
			};
		}

		interface setInterface {
			operator: typeof operator;
			code: number;
			info: {
				Result: "ok" | "fail";
			};
		}

		const res = await this.request<setInterface>(operator, data);
		if (!res || res.code !== 200) {
			this.app.logger.error("机器时间设置失败！ %s ------- %j", this.device.ip, res);
			throw new Error("机器时间设置失败！");
		}
		return true;
	}

	/**
	 * 设置用户名，密码
	 *
	 * 注意设置新用户名密码之后 应使用新的机器配置请求机器
	 *
	 * @param {string} userName
	 * @param {string} password
	 * @return {*}
	 */
	async setUserNameAndPassword(userName: string, password: string) {
		const operator = actions.SetUserPwd;

		const data = {
			info: {
				User: userName,
				Pwd: password,
			},
		};
		const res = await this.request<hc.http.response>(operator, data);
		return res;
	}

	/**
	 * 重启设备
	 *
	 * @return {*}
	 */
	async rebootDevice() {
		const operator = actions.RebootDevice;
		const data = {
			info: {
				IsRebootDevice: 1,
				DeviceID: this.device.deviceId,
			},
		};
		const res = await this.request<hc.http.response>(operator, data);

		return res;
	}

	/**
	 * 远程开门指令
	 *
	 * @param {string} [msg="远程开门"] UI显示信息
	 * @return {*}
	 */
	async openDoor(msg = "远程开门") {
		const operator = actions.OpenDoor;
		const data = {
			info: {
				DeviceID: this.device.deviceId,
				status: 1,
				msg,
			},
		};

		const ctx = this.app.currentContext || {};
		this.app.logger.info(
			"[海清机器，特殊操作日志，后台远程开门] 操作用户 %s 设备id %s",
			ctx?.getUserId() || "unknown",
			this.device.deviceId
		);

		await this.request(operator, data);
		return true;
	}
}
